package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mapper.OrderRegisterAlipayRecMapper;
import com.ruoyi.service.OrderRegisterAlipayRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderRegisterAlipayRecServiceImpl implements OrderRegisterAlipayRecService {

    @Autowired
    OrderRegisterAlipayRecMapper orderRegisterAlipayRecMapper;

    @Override
    public List<Map> getAlipayRecList(Map map) {
        // 前端传过来一个maxDate,默认是当天,然后往上翻5次的封账记录,如果没有或者少于5次,就从头展示
        String maxDate = map.get("maxDate").toString();
        if ("true".equals(map.get("isFlipUp").toString())) {
            // 往上翻的话maxDate要找昨天的
            maxDate = DateUtils.getYesterday(maxDate);
        }
        List<Map> sealList = orderRegisterAlipayRecMapper.getSealList(maxDate);
        Map paramsMap = new HashMap();
        if (sealList.size() > 5) {
            paramsMap.put("maxDate", maxDate);
            paramsMap.put("minDate", sealList.get(4).get("rec_date").toString());
        } else {
            paramsMap.put("maxDate", maxDate);
        }

        // 取最后一次对账
        String maxSealDate = getMaxSealDate();

        // 取银行卡对账记录
        List<Map> wechatRecList = orderRegisterAlipayRecMapper.getAlipayRecList(paramsMap);
        // 起始基数金额
        BigDecimal basePrice = new BigDecimal(0);
        for (Map map1 : wechatRecList) {
            // 支付金额
            BigDecimal paymentPrice = new BigDecimal(map1.get("paymentPrice").toString());
            // 返款金额
            BigDecimal refundPrice = new BigDecimal(map1.get("refundPrice").toString());
            // 转入金额
            BigDecimal inPrice = new BigDecimal(map1.get("inPrice").toString());

            int sealStatus = Integer.parseInt(map1.get("sealStatus").toString());
            if (sealStatus == 0) {
                basePrice = basePrice.subtract(paymentPrice).add(refundPrice).add(inPrice);
                map1.put("sealPrice", basePrice);
            }
            // 一旦发现是封存的日期帐,将初始基数金额改为封装金额
            if (sealStatus == 1) {
                basePrice = new BigDecimal(map1.get("sealPrice").toString());
            }

            String fullDate = map1.get("fullDate").toString().replace("T", " ") + ":00";
            int i = DateUtils.compareTimeSizes(fullDate, maxSealDate);
            if (i == 1) {
                map1.put("sealStatus", 2);
            }
        }
        return wechatRecList;
    }

    @Override
    public Map getAlipayRecById(String id) {
        return orderRegisterAlipayRecMapper.getAlipayRecById(id);
    }

    @Override
    public AjaxResult postEditAlipayRec(Map map) {
        System.out.println(map);
        // 修改时判断是否已经确认对账了
        String fullDate = map.get("fullDate").toString().replace("T", " ");
        String maxSealDate = getMaxSealDate();
        // 必须大于最后一次对账时间
        int i = DateUtils.compareTimeSizes(fullDate, maxSealDate);
        if (i < 1) {
            return AjaxResult.error("该日期已确认对账,无法修改！");
        } else {
            int i1 = orderRegisterAlipayRecMapper.postEditAlipayRec(map);
            if (i1 > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
        }
    }

    @Override
    public int postAddAlipayRec(Map map) {
        return orderRegisterAlipayRecMapper.postAddAlipayRec(map);
    }

    @Override
    public AjaxResult postCancelSeal(Map map) {
        String cancelSealDate = map.get("fullDate").toString().replace("T", " ");
        Map map1 = new HashMap();
        map1.put("cancelSealDate", cancelSealDate);
        map1.put("sealStatus", 0);
        int i1 = orderRegisterAlipayRecMapper.postEditAlipayRec(map1);
        if (i1 > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult postConfirmSeal(Map map) {
        Map map1 = new HashMap();
        String fullDate = map.get("fullDate").toString().replace("T", " ");
        String sealPrice = map.get("sealPrice").toString();
        String manualPrice = map.get("manualPrice").toString();
        map1.put("fullDate", fullDate);
        map1.put("sealPrice", sealPrice);
        map1.put("manualPrice", sealPrice);
        map1.put("sealTime", DateUtils.getDate2());
        map1.put("sealStatus", 1);

        int i = orderRegisterAlipayRecMapper.postEditAlipayRec(map1);
        if (i > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @Override
    public String getMaxSealDate() {
        String maxSealDate = orderRegisterAlipayRecMapper.getMaxSealDate();
        if (maxSealDate == null) {
            maxSealDate = "2024-12-29 00:00:00";
        }
        return maxSealDate;
    }
}
